<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="企业名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入企业名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="信用代码" prop="uniformCode">
        <el-input
          v-model="queryParams.uniformCode"
          placeholder="请输入信用代码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择企业状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusSelect"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="企业法人" prop="concatName" v-if="advanced">
        <el-input
          v-model="queryParams.concatName"
          placeholder="请输入企业法人"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="监管归属" prop="orgId" v-if="advanced">
        <treeselect
          v-model="queryParams.orgId"
          :options="orgOptions"
          style="width: 240px"
          :show-count="true"
          placeholder="请选择归属监管" />
      </el-form-item>
      <el-form-item label="创建时间" v-if="advanced">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <a @click="toggleAdvanced" style="margin-left: 8px">
          {{ advanced ? '收起' : '展开' }}
          <i v-if="!advanced" class="el-icon-arrow-down"></i>
          <i v-if="advanced" class="el-icon-arrow-up"></i>
        </a>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:role:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:role:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:role:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格数据-->
    <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称" align="center" :show-overflow-tooltip="true" width="120">
        <template slot-scope="scope">
          <el-tag type="primary">{{scope.row.companyName}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="信用代码" align="center" prop="uniformCode" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="联系人" align="center" prop="contactName" width="80" />
      <el-table-column label="联系方式" align="center" prop="phone" width="120" />
      <el-table-column label="监管归属" align="center" prop="orgName" width="150" />
      <el-table-column label="地址" align="center" prop="address" width="100" />
      <el-table-column label="状态" align="center" width="80">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:role:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:role:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 底部分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />



    <!-- 添加或修改企业对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="企业名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入企业名称" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="信用代码" prop="uniformCode">
              <el-input v-model="form.uniformCode" placeholder="请输入信用代码" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="企业法人" prop="contactName">
              <el-input v-model="form.contactName" placeholder="请输入企业法人" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属监管" prop="orgId">
              <treeselect v-model="form.orgId" :options="orgOptions" :show-count="true" placeholder="请选择归属监管" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系方式" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择企业状态">
                <el-option
                  v-for="dict in statusSelect"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" type="textarea" placeholder="请输入企业地址"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {addCompany, listCompany, orgTreeList, getCompany, updateCompany, deleteCompany,changeCompanyStatus} from "@/api/subject/company/company";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "index",
  components: { Treeselect },
  data() {
    return {
      showSearch: true,
      // 遮罩层
      loading: true,
      open: false,
      // 总条数
      total: 0,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 表格数据
      companyList: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 所在监管查询
      orgOptions: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: undefined,
        status: undefined,
        uniformCode: undefined,
        concatName: undefined,
        orgId: null
      },
      title: '',
      //新增修改表单
      form: {
        companyId: undefined,
        companyName: undefined,
        uniformCode: undefined,
        contactName: undefined,
        orgId: undefined,
        phone: undefined,
        status: 1,
        address: undefined
      },
      rules: {
        companyName: [
          { required: true, message: "企业名称不能为空", trigger: "blur" }
        ],
        uniformCode: [
          { required: true, message: "信用代码不能为空", trigger: "blur" }
        ],
        contactName: [
          { required: true, message: "企业法人不能为空", trigger: "blur" }
        ],
        orgId: [
          { required: true, message: "所属监管不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系方式不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        address: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ]
      },
      // 日期范围
      dateRange: [],
      statusSelect: [
        {
          label: "正常",
          value: 1
        },
        {
          label: "停用",
          value: 0
        }
      ]
    }
  },
  created() {
    this.getList();
    this.getOrgTree();
  },
  methods: {
    getList(){
      this.loading = true;
      listCompany(this.buildQueryTime(this.queryParams, this.dateRange)).then(response => {
          this.companyList = response.data;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    getOrgTree(){
      orgTreeList().then(response => {
        this.orgOptions = response.data;
      });
    },
    reset(){
        this.form = {
          companyName: undefined,
          uniformCode: undefined,
          contactName: undefined,
          orgId: undefined,
          phone: undefined,
          status: 1,
          address: undefined
        };
      this.resetForm("form");
    },
    submitForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.companyId !== undefined) {
            updateCompany(this.form).then(response =>{
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          } else {
            addCompany(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleAdd(){
      this.reset();
      this.open = true;
      this.title = "添加企业";
    },
    handleUpdate(row){
      this.reset();
      const companyId = row.companyId || this.ids;
      getCompany(companyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业";
      });
    },
    handleDelete(row){
      const companyIds = row.companyId || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return deleteCompany(companyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleExport(){

    },
    handleStatusChange(row) {
      let text = row.status === 1 ? "启用" : "停用";
      this.$modal.confirm('确认要' +text + '当前企业吗？').then(function() {
        return changeCompanyStatus(row.userId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === 0 ? 1 : 0;
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery(){

    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.companyId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    }
  }
}
</script>

<style scoped>

</style>
